package com.example.tianling.admin.controller;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.tianling.admin.params.deleteListDo;
import com.example.tianling.admin.service.ArticleData;
import com.example.tianling.admin.vo.TodayDataVo;
import com.example.tianling.common.annotation.Login;
import com.example.tianling.common.utils.R;
import com.example.tianling.common.vo.TrainArticleVo;
import com.example.tianling.common.vo.article;
import com.example.tianling.common.vo.forumVo;
import com.example.tianling.service.ForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
public class ArticleDataController {
    //根据id 返回对应的数据
    @Autowired
    private ArticleData articleData ;
    @Autowired
    private ForumService forumService;

    @PostMapping("/admin/article")
    @ResponseBody
    public R loadArticleAnalyse() {
        //这个我感觉要返回当天的发表文字的信息，有每个类型发帖数，发帖总数
        TodayDataVo todayDataVo = articleData.load();
        return R.success(todayDataVo);
    }


    @Login
    @PostMapping("/admin/article.selectDate.do")
    @ResponseBody
    public R ArticleDateAnalyse(@RequestBody String params) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String startTime = paramsJSONObject.getString("startTime");
        String endTime = paramsJSONObject.getString("endTime");
        //这个返回选择时间段的帖子的总数，这个时间段每天每个类型的发帖数
        List result = articleData.loadArticleIndate(startTime,endTime);
        return R.success(result);
    }
    @Login
    @PostMapping("/admin/article.searchArticle.do")
    @ResponseBody
    public R sreachArticle(@RequestBody String params) {
        //搜索帖子（搜索有两个类型，一个按标题，一个按用户名字），显示列表
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String type = paramsJSONObject.getString("type");
        String keyword = paramsJSONObject.getString("keyword");
        if(Integer.valueOf(type) == 1){
            IPage<article> learnArticle= forumService.search(1,1,keyword);
            IPage<article> chatArticle= forumService.search(1,2,keyword);
            IPage<TrainArticleVo> trainArticle= forumService.searchTrain(1,3,keyword);
            return R.success(new forumVo(learnArticle,chatArticle,trainArticle));
        }
        else {
            IPage<article> learnArticle= articleData.searchAuthorArticle(1,1,keyword);
            IPage<article> chatArticle= articleData.searchAuthorArticle(1,2,keyword);
            IPage<TrainArticleVo> trainArticle= articleData.searchAuthorTrain(1,3,keyword);
            return R.success(new forumVo(learnArticle,chatArticle,trainArticle));
        }
    }
    @Login
    @PostMapping("/admin/article.searchArticleDonwLoad.do")
    @ResponseBody
    public R sreachArticleDonwLoad(@RequestBody String params) {
        //搜索帖子（搜索有两个类型，一个按标题，一个按用户名字），显示列表
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String type = paramsJSONObject.getString("type");
        String keyword = paramsJSONObject.getString("keyword");
        Integer page = Integer.valueOf(paramsJSONObject.getString("page"));
        Integer ArticleType = Integer.valueOf(paramsJSONObject.getString("ArticleType"));
        if(Integer.valueOf(type) == 1){
            System.out.println("dassda");
            if (ArticleType==1) {
                IPage<article> newArticle= forumService.search(page,1,keyword);
                return R.success(new forumVo(newArticle,null,null));
            }
            else if(ArticleType==2){
                IPage<article> newArticle= forumService.search(page,2,keyword);
                return R.success(new forumVo(null,newArticle,null));
            }
            else{
                IPage<TrainArticleVo> newArticle= forumService.searchTrain(page,3,keyword);
                return R.success(new forumVo(null,null,newArticle));
            }
        }
        else {
            if (ArticleType==1) {
                IPage<article> newArticle= articleData.searchAuthorArticle(page,1,keyword);
                return R.success(new forumVo(newArticle,null,null));
            }
            else if(ArticleType==2){
                IPage<article> newArticle= articleData.searchAuthorArticle(page,2,keyword);
                return R.success(new forumVo(null,newArticle,null));
            }
            else{
                IPage<TrainArticleVo> newArticle= articleData.searchAuthorTrain(page,3,keyword);
                return R.success(new forumVo(null,null,newArticle));
            }
        }
    }
    @Login
    @PostMapping("/admin/article.deleteArticle.do")
    @ResponseBody
    public R deleteArticle(@RequestBody deleteListDo deleteListDo) {
        //返回一个列表，逐个删除列表
        Integer result = articleData.deleteArticle(deleteListDo.getDeleteList());
        return new R(200,"已成功删除"+result+"条数据");
    }
}
